package au.com.evoCoach.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import au.com.evoCoach.domain.PlayerForRound;
import au.com.evoCoach.domain.Round;
import au.com.evoCoach.domain.Team;

@Repository
public class PlayerForRoundDAO extends BaseDAO {

	private RoundDAO roundDAO;
    
	@Autowired
	public PlayerForRoundDAO(RoundDAO roundDAO) {
		super(PlayerForRound.class);
		
		this.roundDAO = roundDAO;
	}
	
	public List<PlayerForRound> getPlayersForRound(Team team, Round round) {
	    String sql = "select pfr from PlayerForRound pfr join pfr.teamPlayer tp where tp.team = :team and pfr.round = :round order by position";
	    Query query = getSession().createQuery(sql);
	    query.setParameter("team", team);
	    query.setParameter("round", round);
    
	    return query.list();        
	}
	
	public void deletePlayersForRound(Team team) {
	    Round round = roundDAO.getCurrentRound();
	    String sql = "delete from PlayerForRound pfr where pfr.teamPlayer in (from TeamPlayer where team = :team) and pfr.round = :round";
        Query query = getSession().createQuery(sql);
        query.setParameter("team", team);
        query.setParameter("round", round);
    
        query.executeUpdate();
	}
	
}
